package com.pactera.madp.cp.controller.oms.workroom;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.em.permission.ReqPerEnum;
import com.pactera.madp.cp.api.em.workupdatetime.TypeEnum;
import com.pactera.madp.cp.api.entity.workroom.WorkRoomGroupEntity;
import com.pactera.madp.cp.api.dto.workroom.WorkRoomIndexDto;
import com.pactera.madp.cp.api.dto.workroom.WorkRoomStatisticsIndexDto;
import com.pactera.madp.cp.api.vo.taskpublish.GroupChatVO;
import com.pactera.madp.cp.api.vo.workroom.WorkRoomDataStatisticsVO;
import com.pactera.madp.cp.api.vo.workroom.WorkRoomPageVO;
import com.pactera.madp.cp.service.*;
import com.pactera.madp.cp.service.impl.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.*;

/**
 * @description:客户群
 * @author: Huayu
 * @time: 2020/12/8 14:26
 */

@RestController
@AllArgsConstructor
@RequestMapping("/workroom")
@Api(value = "workroom", tags = "客户群")
public class WorkRoomController {

    private IWorkRoomService workRoomServiceImpl;
    private IWorkRoomGroupService workRoomGroupServiceImpl;
    private IWorkUpdateTimeService workUpdateTimeService;

    /**
     * @description:客户群管理-列表
     * @return:
     * @author: Huayu
     * @time: 2020/12/11 15:24
     */
    @GetMapping("/page")
    @ApiOperation(value = "客户群管理-列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="permission",value="权限:ALL-全部,DEPARTMENT-部门,EMPLOYEE-员工")
    })
    public R workRoomIndex(WorkRoomIndexDto workRoomIndexDto, ReqPerEnum permission, Page page) {
        return workRoomServiceImpl.getWorkRoomList(workRoomIndexDto, permission, page);
    }

    /**
     * @description:客户群管理-同步群
     * @return:
     * @author: Huayu
     * @time: 2020/12/11 15:28
     */
    @SysLog("客户群管理-同步群")
    @PutMapping("/syn")
    @ApiOperation(value = "客户群管理-同步群")
    @ApiImplicitParams({
            @ApiImplicitParam(name="corpId",value="企业id",dataType ="String")
    })
    public R workRoomSync(String corpId, HttpServletRequest request) {
        if (corpId == null || corpId.equals("")) {
            return R.failed("未选择登录企业，不可操作");
        }
        Integer corpIds = AccountService.getCorpId();
        //同步企业客户群聊
        boolean flag = false;
        try {
            flag = workRoomServiceImpl.syncWorkRoomIndex(corpIds, null, 0);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (flag) {
            //更新同步时间
            workUpdateTimeService.updateSynTime(Integer.valueOf(corpId), TypeEnum.WORK_ROOM);
            return R.ok();
        }
        return R.failed("该客户群数据错误");
    }

    /**
     * @description:统计分页数据
     * @return:
     * @author: Huayu
     * @time: 2020/12/22 8:59
     */
    @GetMapping("statisticsindex")
    @ApiOperation(value = "统计分页数据")
    public R statisticsIndex(WorkRoomStatisticsIndexDto workRoomStatisticsIndex, Page page){
       return workRoomServiceImpl.statisticsIndex(workRoomStatisticsIndex,page);
    }

    /**
     * @description:统计折线图
     * @return:
     * @author: Huayu
     * @time: 2020/12/14 14:30
     */
    @GetMapping("/statistics")
    @ApiOperation(value = "统计折线图")
    public R statistics(WorkRoomStatisticsIndexDto workRoomStatisticsIndexDto){
        return workRoomServiceImpl.statistics(workRoomStatisticsIndexDto);
    }

    @SysLog("客户群管理-批量修改")
    @PutMapping("/batchupdate")
    @ApiOperation(value = "批量修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name="workRoomGroupId",value="客户群分组id",dataType ="int"),
            @ApiImplicitParam(name="workRoomIds",value="客户群ids",dataType ="String")
    })
    public R batchUpdate(int workRoomGroupId,String workRoomIds) {
//        int workRoomGroupId = req.getIntValue("workRoomGroupId");
//        String workRoomIds = req.getString("workRoomIds");
        if (!StringUtils.hasLength(workRoomIds)) {
            throw new CheckedException("workRoomIds 不能为空");
        }

        int corpId = AccountService.getCorpId();

        //验证客户群分组的有效性
        if (workRoomGroupId != 0) {
            WorkRoomGroupEntity workRoomGroupEntity = workRoomGroupServiceImpl.getWorkRoomGroupById(workRoomGroupId);
            if (workRoomGroupEntity == null) {
                throw new CheckedException("该分组信息不存在，不可操作");
            }
            if (!workRoomGroupEntity.getCorpId().equals(corpId)) {
                throw new CheckedException("该分组不归属当前登录企业，不可操作");
            }
        }

        String[] roomIdArr = workRoomIds.split(",");
        //数据入表
        for (String roomId : roomIdArr) {
            workRoomServiceImpl.updateWorkRoomsByRoomGroupId(Integer.valueOf(roomId), workRoomGroupId);
        }
        return R.ok();
    }

    /**
     * @description 客户 - 群聊列表下拉框
     * @author zhaojinjian
     * @createTime 2020/12/30 16:19
     */
    @GetMapping("/roomindex")
    @ApiOperation(value = "群聊列表下拉框")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value="客户群名称",dataType ="String"),
            @ApiImplicitParam(name="roomGroupId",value="分组id",dataType ="Integer"),
            @ApiImplicitParam(name="current",value="当前页",dataType ="Integer"),
            @ApiImplicitParam(name="size",value="分页大小",dataType ="Integer")
    })
    public R getRoomIndex(String name, Integer roomGroupId,Integer current,Integer size) {
        Integer corpId = AccountService.getCorpId();
        if (corpId == null) {
            return R.failed("未选择登录企业，不可操作");
        }
        return R.ok(workRoomServiceImpl.getWorkRoomSelectData(corpId, name, roomGroupId,current,size));
    }

    @GetMapping("/getworkroom")
    @ApiOperation(value = "获取群聊列表")
    public R<List<GroupChatVO>> getWorkRoom(){
        return R.ok( workRoomServiceImpl.getWorkRoomName());
    }
    @GetMapping("/getroom")
    @ApiOperation(value = "获取群聊列表")
    public R<List<GroupChatVO>> getroom(){
        return R.ok( workRoomServiceImpl.getWorkRoomName());
    }

    @GetMapping("/statistics-overview")
    @ApiOperation(value = "华融湘江客户群统计基础信息")
    public R<WorkRoomDataStatisticsVO> getStatisticsData() {
        return R.ok(workRoomServiceImpl.getStatisticsData());
    }

    @GetMapping("/hold-room-page")
    @ApiOperation(value = "华融湘江成员持群信息分页查询")
    public R<IPage<WorkRoomPageVO>> getHoldRoomPage(Page page,String ownerName) {
        return R.ok(workRoomServiceImpl.getHoldRoomPage(page,ownerName));
    }

}
